//
//  DMRegisterVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/14.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMRegisterVC.h"
#import "DMSucceedVC.h"

//view
#import "KCountdownButton.h"


//request
#import "DMLoginAndRegisterRequest.h"

@interface DMRegisterVC ()
@property (weak, nonatomic) IBOutlet UITextField *inviteTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *SMSCodeTF;
@property (weak, nonatomic) IBOutlet UIButton *selectProtcolBtn;

@end

@implementation DMRegisterVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    self.navigationItem.title = @"注册";
}

#pragma mark ----action
//扫描
- (IBAction)scanAction:(UIButton *)sender {
    
    
}
//发送验证码
- (IBAction)sendSMSCodeAction:(KCountdownButton *)sender {
    
    if (self.phoneNumTF.text.length != 11) {
        [JLProgressHUDManager showErrorHud:@"请输入正确的手机号码"];
        return;
    }
    
    
    DMLoginAndRegisterRequest * request = [[DMLoginAndRegisterRequest alloc]initPhoneSMSCodeWithPhone:self.phoneNumTF.text];
    
    //开始倒计时
    [JLProgressHUDManager showLoadingHud];
    
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        [JLProgressHUDManager showSuccessHud:baseModel.msg];
        
        [sender startCountdown];

    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
    }];
    
    
    
}

//注册
- (IBAction)registerAction:(UIButton *)sender {
    if (self.phoneNumTF.text.length == 0) {
        [JLProgressHUDManager showErrorHud:@"请输入验证码"];
        return;
    }
    
    if (self.phoneNumTF.text.length == 0) {
        [JLProgressHUDManager showErrorHud:@"请输入手机号码"];
        return;
    }
    
    if (self.passwordTF.text.length == 0) {
        [JLProgressHUDManager showErrorHud:@"请输入密码"];
        return;
    }
    
    if (self.SMSCodeTF.text.length == 0) {
        [JLProgressHUDManager showErrorHud:@"请输入验证码"];
        return;
    }
    
    if (!self.selectProtcolBtn.isSelected) {
        [JLProgressHUDManager showErrorHud:@"请阅读协议"];
        return;
    }
    
    NSString * pwdMD5Str = [self.passwordTF.text md5String];
    
    DMLoginAndRegisterRequest * request = [[DMLoginAndRegisterRequest alloc]initRegisterWithInviteCode:self.inviteTF.text phone:self.phoneNumTF.text password:pwdMD5Str smsCode:self.SMSCodeTF.text];
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        [JLProgressHUDManager showSuccessHud:@"注册成功"];
        DMSucceedVC * vc = [[DMSucceedVC alloc]init];
        vc.typeIndex = registerSucceed;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    
    } failure:^(KBaseModel *errorModel) {
        [JLProgressHUDManager showErrorHud:errorModel.msg];
    }];
    
}

//阅读协议
- (IBAction)readProtocolAction:(UIButton *)sender {
    
    
}

//选择协议
- (IBAction)selectProtocolAction:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
}

@end
